package thread;

public class Demo10 {
    public static void main(String[] args) {
        Object lock1=new Object();
        Object lock2=new Object();
        Object lock3=new Object();
        Thread thread1 =new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (lock1) {
                        lock1.wait();
                    }
                    System.out.print("A");
                    //通知线程2结束等待
                    synchronized (lock2) {
                        lock2.notify();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread thread2 =new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (lock2) {
                        lock2.wait();

                    }System.out.print('B');
                    synchronized (lock3){
                        lock3.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread thread3 =new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (lock3) {
                        lock3.wait();

                    } System.out.print('C');
                    synchronized (lock1){
                        lock1.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
        //避免notify了还没wait
        //手动启动第一个A的打印
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (lock1){
            lock1.notify();
        }
    }
}
